#include "playercontrols.h"

#include <QBoxLayout>
#include <QSlider>
#include <QStyle>
#include <QToolButton>
#include <QComboBox>
#include <QAudio>
#include <QTime>

PlayerControls::PlayerControls(QWidget *parent)
    : QWidget(parent)
    , playerState(QMediaPlayer::StoppedState)
    , playerMuted(false)
{

}

void PlayerControls::setPlayer(QMediaPlayer *player)
{
    setState(player->state());
    setVolume(player->volume());
    setMuted(isMuted());

    m_player = player;

    connect(this, SIGNAL(play()), m_player, SLOT(play()));
    connect(this, SIGNAL(pause()), m_player, SLOT(pause()));
    connect(this, SIGNAL(stop()), m_player, SLOT(stop()));
    connect(this, SIGNAL(changeVolume(int)), m_player, SLOT(setVolume(int)));
    connect(this, SIGNAL(changeMuting(bool)), m_player, SLOT(setMuted(bool)));
    connect(this, SIGNAL(changeRate(qreal)), m_player, SLOT(setPlaybackRate(qreal)));

    connect(m_player, SIGNAL(stateChanged(QMediaPlayer::State)),
            this, SLOT(setState(QMediaPlayer::State)));
    connect(m_player, SIGNAL(volumeChanged(int)), this, SLOT(setVolume(int)));
    connect(m_player, SIGNAL(mutedChanged(bool)), this, SLOT(setMuted(bool)));
    connect(m_player, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64)));
    connect(m_player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
}

QMediaPlayer::State PlayerControls::state() const
{
    return playerState;
}

void PlayerControls::setState(QMediaPlayer::State state)
{
    if (state != playerState) {
        playerState = state;
        emit stateChanged();
   }
}

void PlayerControls::playClicked()
{
    switch (playerState) {
    case QMediaPlayer::StoppedState:
    case QMediaPlayer::PausedState:
        emit play();
        break;
    case QMediaPlayer::PlayingState:
        emit pause();
        break;
    }
}
bool PlayerControls::isPlay() const
{
    return playerState == QMediaPlayer::PlayingState;
}

int PlayerControls::volume() const
{
    return m_volume;
}

void PlayerControls::setVolume(int volume)
{
    if (volume != m_volume) {
        m_volume = volume;
        emit changeVolume(volume);
    }
}


bool PlayerControls::isMuted() const
{
    return playerMuted;
}

void PlayerControls::setMuted(bool muted)
{
    if (muted != playerMuted) {
        playerMuted = muted;
        emit changeMuting(playerMuted);
    }
}

void PlayerControls::muteClicked()
{
    playerMuted = !playerMuted;
    emit changeMuting(playerMuted);
}

qreal PlayerControls::playbackRate() const
{
    return m_rate;
}

void PlayerControls::setPlaybackRate(float rate)
{
    if(rate != m_rate){
        m_rate = rate;
        emit changeRate(rate);
    }
}
qint64 PlayerControls::getDuration()
{
    return m_duration;
}
void PlayerControls::durationChanged(qint64 duration)
{
    m_duration = duration/1000;
    emit changeDurationLabel(m_duration);
}

void PlayerControls::positionChanged(qint64 progress)
{
    qint64 seconds = progress/1000;
    if(m_progress != seconds){
        m_progress = progress/1000;
        emit changePositionLabel(m_progress);
        updateDurationInfo(m_progress);
    }
}

void PlayerControls::seek(int seconds)
{
    if(m_progress != seconds){
        m_player->setPosition(seconds * 1000);
    }
}

void PlayerControls::updateDurationInfo(qint64 currentInfo)
{
    QString tStr;
    if (currentInfo || m_duration) {
        QTime currentTime((currentInfo/3600)%60, (currentInfo/60)%60, currentInfo%60, (currentInfo*1000)%1000);
        QTime totalTime((m_duration/3600)%60, (m_duration/60)%60, m_duration%60, (m_duration*1000)%1000);
        QString format = "mm:ss";
        if (m_duration > 3600)
            format = "hh:mm:ss";
        tStr = currentTime.toString(format) + " / " + totalTime.toString(format);
    }
    emit changeDurationInfo(tStr);
}


